class Solution {
public:
    int monotoneIncreasingDigits(int n) {

        string s = to_string(n);
        if (s.size() == 1) return n;
        int index;
        int flag = 0;
        for (int i = 0;i <= s.size() - 2;i++)
        {
            if (s[i + 1] < s[i])
            {
                index = i;
                flag = 1;
                break;
            }
        }
        char tmp = s[index];
        if (flag == 0)
        {
            return n;
        }

        while (index > 0 && s[index - 1] == tmp)
        {
            index--;
        }
        //cout<<index<<endl;
        s[index]--;
        for (int i = index + 1;i < s.size();i++)
        {
            s[i] = '9';
        }
        return stoi(s);

    }
};